<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="getData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="showAddDlog">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-tabs v-model="seachForm.type" type="card" @tab-click="saveActive">
            <el-tab-pane :label="$t('panttype')" name="pantsPattern">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>裤子</el-breadcrumb-item>
                        <el-breadcrumb-item>裤型</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="typeTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="name" :label="$t('name')">
                        </el-table-column>
                        <el-table-column prop="bh" :label="$t('serialnumber')">
                        </el-table-column>
                        <el-table-column :label="$t('image')">
                            <template v-slot="scope">
                                <el-image v-if="scope.row.imgUrl !== '' && scope.row.imgUrl !== null" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModifyType(scope.row)" size="mini">编辑</el-button>
                                <el-button @click.stop="showDelType(scope.row.uuid)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        small
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :pager-count="11"
                        :current-page.sync="seachForm.pageNo"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="seachForm.pageSize"
                        layout="sizes, prev, pager, next"
                        :total="total">
                    </el-pagination>
                </el-card>
            </el-tab-pane>
            <el-tab-pane :label="$t('pocket')" name="pantsPkt">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>裤子</el-breadcrumb-item>
                        <el-breadcrumb-item>口袋</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="pocketTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="name" :label="$t('name')">
                        </el-table-column>
                        <el-table-column prop="bh" :label="$t('serialnumber')">
                        </el-table-column>
                        <el-table-column :label="$t('image')">
                            <template v-slot="scope">
                                <el-image v-if="scope.row.imgUrl !== '' && scope.row.imgUrl !== null" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModifyType(scope.row)" size="mini">编辑</el-button>
                                <el-button @click.stop="showDelType(scope.row.uuid)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-tab-pane>
            <el-tab-pane :label="$t('waist')" name="pantsWasit">
                <el-card class="cardBox">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item>裤子</el-breadcrumb-item>
                        <el-breadcrumb-item>腰部</el-breadcrumb-item>
                    </el-breadcrumb>
                    <el-table style="width: 100%" :data="waistTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="name" :label="$t('name')">
                        </el-table-column>
                        <el-table-column prop="bh" :label="$t('serialnumber')">
                        </el-table-column>
                        <el-table-column :label="$t('image')">
                            <template v-slot="scope">
                                <el-image v-if="scope.row.imgUrl !== '' && scope.row.imgUrl !== null" :src="scope.row.imgUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" width="150" :label="$t('operation')">
                            <template v-slot="scope">
                                <el-button @click.stop="showModifyType(scope.row)" size="mini">编辑</el-button>
                                <el-button @click.stop="showDelType(scope.row.uuid)" size="mini">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-tab-pane>
            <!-- <el-tab-pane :label="$t('size')" name="size">
                <el-card class="cardBox">
                    <div class="btnList">
                        <el-button size="mini" @click="addSizeDialogVisible = true">{{$t('add')}}</el-button>
                    </div>
                    <el-table style="width: 100%" :data="sizeTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                        <el-table-column prop="size" :label="$t('size')">
                        </el-table-column>
                        <el-table-column prop="address" width="120" :label="$t('operation')">
                            <template v-slot="scope">
                                <img src="@/assets/edit.png" :alt="$t('picisgone')" @click="showModifySize(scope.row)">
                                <img src="@/assets/remove.png" :alt="$t('picisgone')" @click="showDelSize(scope.row.id)">
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-tab-pane> -->
        </el-tabs>
        <!-- 裤型 -->
        <el-dialog :title="$t('add')" :visible.sync="addDataDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addTypeDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addDataRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('serialnumber')" label-width="70px" prop="bh">
                    <el-input v-model="addDataForm.bh" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDataDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addTypeData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="$t('modify')" :visible.sync="modifyDataDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyTypeDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyDataRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="modifyDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('serialnumber')" label-width="70px" prop="bh">
                    <el-input v-model="modifyDataForm.bh" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="handleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDataDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyTypeData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="$t('hint')" :visible.sync="delDataDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDataDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delTypeData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getRobesElementAll,
    addRobesElementItem,
    editRobesElementItem,
    deleteRobesElementItem
} from "@/libs/reqlist.js"
export default {
    data () {
        return {
            total: 0,
            seachForm: {
                deleteStatus: 0,
                pageNo: 1,
                pageSize: 10,
                name: '',
                bh: '',
                type: 'pantsPattern',
                cloType: '2'
            },
            // 裤型
            typeTab: [],
            // 口袋
            pocketTab: [],
            // 腰部
            waistTab: [],
            // 尺码
            sizeTab: [],
            imgList: [],
            doType: 0,
            addDataDialogVisible: false,
            addDataForm: {
                name: '',
                bh: '',
                type: '',
                cloType: '2',
                imgUrl: '',
                uuid: ''
            },
            addDataRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                bh: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            modifyDataDialogVisible: false,
            fileList: [],
            modifyDataForm: {
                name: '',
                bh: '',
                type: '',
                cloType: '2',
                imgUrl: '',
                uuid: ''
            },
            modifyDataRules: {},
            delById: '',
            delDataDialogVisible: false
        }
    },
    methods: {
        saveActive(tab) {
            this.seachForm.type = tab.name
            this.seachData()
            window.sessionStorage.setItem('pantAct', this.activeName)
        },
        seachData () {
            this.imgList = []
            getRobesElementAll(this.seachForm).then(res => {
                if (res.data.code == 0) {
                    this.total = res.data.data.hits
                    switch (this.seachForm.type) {
                        case 'pantsPattern':
                            res.data.data.data.forEach(item => {
                                if (item.imgUrl !== '' && item.imgUrl !== null) this.imgList.push(item.imgUrl)
                            })
                            this.typeTab = res.data.data.data
                            break
                        case 'pantsPkt':
                            res.data.data.data.forEach(item => {
                                if (item.imgUrl !== '' && item.imgUrl !== null) this.imgList.push(item.imgUrl)
                            })
                            this.pocketTab = res.data.data.data
                            break
                        case 'pantsWasit':
                            res.data.data.data.forEach(item => {
                                if (item.imgUrl !== '' && item.imgUrl !== null) this.imgList.push(item.imgUrl)
                            })
                            this.waistTab = res.data.data.data
                            break
                    }
                }
            })
        },
        getData() {
            this.seachForm.pageNo = 1
            this.seachData()
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        getSrcList(index) {
            return this.imgList.slice(index).concat(this.imgList.slice(0, index))
        },
        handleImg(res) {
            if (res.code == 0) {
                switch (this.doType) {
                    case 1:
                        this.addDataForm.imgUrl = res.data
                        break
                    case 2:
                        this.modifyDataForm.imgUrl = res.data
                        break
                }
            }
        },
        handleRemove(file) {
            if (file) {
                switch (this.doType) {
                    case 1:
                        this.fileList = []
                        this.addDataForm.imgUrl = ''
                        break
                    case 2:
                        this.fileList = []
                        this.modifyDataForm.imgUrl = ''
                        break
                }
            }
        },
        // 上传文件超出个数触发
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        // 裤型
        getTypeData () {},
        showAddDlog() {
            this.doType = 1
            this.addDataForm.type = this.seachForm.type
            this.addDataDialogVisible = true
        },
        addTypeDlogClose (form) {
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        addTypeData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.addDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                addRobesElementItem(JSON.stringify(this.addDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.addDataDialogVisible = false
                        this.$message.success(this.$t('sucadd'))
                        this.seachData()
                    }
                })
            })
        },
        showModifyType (data) {
            this.doType = 2
            this.modifyDataForm.name = data.name
            this.modifyDataForm.bh = data.bh
            this.modifyDataForm.type = data.type
            this.modifyDataForm.cloType = data.cloType
            this.modifyDataForm.imgUrl = data.imgUrl
            if (data.imgUrl !== '' && data.imgUrl !== null) this.fileList.push({ name: data.name, url: data.imgUrl })
            this.modifyDataForm.uuid = data.uuid
            this.modifyDataDialogVisible = true
        },
        modifyTypeDlogClose (form) {
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        modifyTypeData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                if (this.modifyDataForm.imgUrl == '') return this.$message.warning('请至少选择一张图片上传')
                editRobesElementItem(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.modifyDataDialogVisible = false
                        this.$message.success(this.$t('modifysuc'))
                        this.seachData()
                    }
                })
            })
        },
        showDelType(uuid) {
            this.delById = uuid
            this.delDataDialogVisible = true
        },
        delTypeData() {
            deleteRobesElementItem({ uuid: this.delById }).then(res => {
                if (res.data.code == 0) {
                    this.delDataDialogVisible = false
                    this.$message.success(this.$t('delsuc'))
                    this.seachData()
                }
            })
        }
    },
    mounted () {
        if (window.sessionStorage.getItem('pantAct')) this.activeName = window.sessionStorage.getItem('pantAct')
        this.seachData()
    },
    destroyed () {
        if (window.sessionStorage.getItem('pantAct')) window.sessionStorage.removeItem('pantAct')
    }
}
</script>

<style lang="less" scoped>
@import './pants.less';
</style>
